package com.honglu.backend.service.impl;

import com.honglu.backend.dao.AppVersionDao;
import com.honglu.backend.entity.AppVersion;
import com.honglu.backend.service.AppVersionService;
import com.honglu.backend.vo.AppVersionVo;
import com.honglu.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tangbin on 2017/6/16.
 */
@Service
public class AppVersionServiceImpl implements AppVersionService {

    @Autowired
    private AppVersionDao appVersionDao;

    @Override
    public List<AppVersion> selectList(AppVersionVo vo) {
        Map<String, Object> params = new HashMap<>();
        params.put("rows", vo.getPageIndex());
        params.put("size", vo.getPageSize());
        List<AppVersion> list = appVersionDao.selectEntity(params);
        return list;
    }

    @Override
    public Integer selectCount(AppVersionVo vo) {
        Map<String, Object> params = new HashMap<>();
        Integer count = appVersionDao.selectCount(params);
        return count;
    }

    @Override
    public Boolean insert(AppVersionVo vo) {
        AppVersion appVersion = new AppVersion();
        appVersion.setAppVersionId(UUIDUtils.getUUID());
        appVersion.setAppType(vo.getAppType());
        appVersion.setForcedUpdate(vo.getForcedUpdate());
        appVersion.setVersionNumber(vo.getVersionNumber());
        appVersion.setDownloadUrl(vo.getDownloadUrl());
        appVersion.setChangeDesc(vo.getChangeDesc());
        appVersion.setState(vo.getState());
        appVersion.setTag(vo.getTag());
        appVersion.setPopup(vo.getPopup());
        appVersion.setBeginTime(vo.getBeginTime());
        appVersion.setEndTime(vo.getEndTime());
        appVersion.setRemark(vo.getRemark());
        return appVersionDao.saveEntity(appVersion) > 0;
    }

    @Override
    public Boolean delete(String appVersionId) {
        AppVersion appVersion = new AppVersion();
        appVersion.setAppVersionId(appVersionId);
        appVersion.setState(9);
        return appVersionDao.updateEntity(appVersion) > 0;
    }

    @Override
    public AppVersion selectEntity(String appVersionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("appVersionId", appVersionId);
        List<AppVersion> appVersion = appVersionDao.selectEntity(params);
        if (!CollectionUtils.isEmpty(appVersion)) {
            return appVersion.get(0);
        }
        return null;
    }

    @Override
    public Boolean modify(AppVersionVo vo) {
        AppVersion appVersion = new AppVersion();
        appVersion.setAppVersionId(vo.getAppVersionId());
        appVersion.setAppType(vo.getAppType());
        appVersion.setForcedUpdate(vo.getForcedUpdate());
        appVersion.setVersionNumber(vo.getVersionNumber());
        appVersion.setDownloadUrl(vo.getDownloadUrl());
        appVersion.setChangeDesc(vo.getChangeDesc());
        appVersion.setState(vo.getState());
        appVersion.setTag(vo.getTag());
        appVersion.setPopup(vo.getPopup());
        appVersion.setBeginTime(vo.getBeginTime());
        appVersion.setEndTime(vo.getEndTime());
        appVersion.setRemark(vo.getRemark());
        return appVersionDao.updateEntity(appVersion) > 0;
    }
}
